package com.cgtong.android.mvp.presenter;

import com.cgtong.android.bean.ProblemBean;
import com.cgtong.android.http.ConstantManager;
import com.cgtong.android.http.callback.ProblemPageCallBack;
import com.cgtong.android.mvp.contract.HisProblemsContract;
import com.cgtong.android.mvp.model.HisProblemsModel;
import com.zhouyou.http.exception.ApiException;

import java.util.List;

/**
 * Created by chunc on 2017/10/27.
 */

public class HisProblemsPresenter implements HisProblemsContract.Presenter {

    private HisProblemsContract.Model mModle;
    private HisProblemsContract.View mView;

    public HisProblemsPresenter(HisProblemsContract.View view) {
        this.mView = view;
        mModle = new HisProblemsModel();
    }

    @Override
    public void httpProblemsData(long maxId, long userId, final boolean isRefresh) {
        if (isRefresh) {
            mView.setEnableLoadMore(false);
            mView.setRefreshing(true);
        }
        mModle.httpProblemsData(maxId, userId, new ProblemPageCallBack<List<ProblemBean>>() {
            @Override
            public void onHttpProblemsSuccess(List<ProblemBean> problemBeen) {
                if (mView != null) {
                    if (isRefresh) {
                        mView.setRefreshing(false);
                        mView.setEnableLoadMore(true);
                        if (problemBeen == null || problemBeen.size() == 0) {
                            mView.onNullData();
                            return;
                        } else if (problemBeen.size() < ConstantManager.USER_FOLLOWED_PAGE_SIZE_INT) {
                            mView.onLoadMoreEnd(isRefresh);
                        }
                        mView.onHttpProblemsDataSuccess(problemBeen, isRefresh);
                    } else {
                        mView.onHttpProblemsDataSuccess(problemBeen, isRefresh);
                        if (problemBeen.size() < ConstantManager.PROBLEMS_PAGE_SIZE) {
                            mView.onLoadMoreEnd(isRefresh);
                        }
                    }
                }
            }


            @Override
            public void onError(ApiException e) {
                if (mView != null) {
                    mView.setRefreshing(false);
                    mView.onError(e);
                }
            }
        });
    }

    @Override
    public void onDestroy() {
        if (mView != null)
            mView = null;
    }
}
